<?php

namespace App\Http\Controllers;

//使用道具
use App\Logic\QuicklyGrabLogic;

class UseItem extends Base
{

    public function logic()
    {
        if (!isset($this->param['uid'])) {
            return $this->returnError(1, "please input uid");
        }
        if (!isset($this->param['item_id'])) {
            return $this->returnError(2, "please input item_id");
        }
        $uid     = $this->param['uid'];
        $item_id = $this->param['item_id'];
        $data    = array();
        //获取物品信息
        $item_info = $this->get_item_info($item_id);
        if ($item_info == false) {
            return $this->returnError(3, "未知的物品:" . (string)$item_id);
        }
        //获取用户信息
        $user_info = $this->get_user_info($uid);
        if ($user_info == false) {
            return $this->returnError(10, "未知的用户:" . (string)$uid);
        }
        $type         = (int)$item_info['type'];
        $data['type'] = $type;
        //先减去一个物品
        $ret = $this->sub_user_item($uid, $item_id, 1);
        if (!$ret) {
            return $this->returnError(9, "物品不足");
        } else {
            $ret = false;
        }
        switch ($type) {
            //是系统等价物
            case 0: {
                return $this->returnError(4, "这个物品不能被使用");
                break;
            }
            //是抽奖礼包
            case 1: {
                $reward_id = $this->rand_reward((int)$item_info['rand_rule']);
                if ($reward_id[0] < 0) {
                    return $this->returnError(5, "抽奖发生错误 , code:" . (string)$reward_id[0]);
                } else {
                    $ret = $this->reward_item($uid, $reward_id[0], 1, "礼包抽奖", "item_id:" . (string)$item_id);
                    if ($ret) {
                        if ($reward_id[1]) {
                            $reward_info = $this->get_item_info($reward_id[0]);
                            if ($item_id == 25008) {
                                $this->add_notice("恭喜玩家" . $user_info['nick_name'] . "转动轮盘获得道具【" . $reward_info['name'] . "】1个");
                            } else {
                                $this->add_notice("恭喜玩家" . $user_info['nick_name'] . "使用【" . $item_info['name'] . "】获得道具【" . $reward_info['name'] . "】1个");
                            }
                            $data['reward_info']['is_notice'] = $reward_id[1];
                        }
                        $data['reward_info']['id'] = $reward_id[0];
                    }
                }
                break;
            }
            //是金币礼包
            case 2: {
                $ret            = $this->grant_gold($uid, (int)$item_info['reward'], "金币礼包", "item_id:" . (string)$item_id);
                $data['reward'] = (int)$item_info['reward'];
                break;
            }
            //是钻石礼包
            case 3: {
                $ret            = $this->grant_diamonds(
                    $uid,
                    (int)$item_info['reward'],
                    "兑换钻石礼包",
                    "item_id:" . (string)$item_id
                );
                $data['reward'] = (int)$item_info['reward'];
                break;
            }
            //是商家优惠券
            case 4: {
                return $this->returnError(4, "This item can't be used!");
                break;
            }
            //房卡道具
            case 5:
                app(Base::class)->grant_room_ticket($uid,  (int)$item_info['reward']);
                $data['reward'] = (int)$item_info['reward'];
                $ret = true;
                break;
            //快抢赛道具
            case 6:
                $num = (int)$item_info['reward'];
                QuicklyGrabLogic::instance()->updateOrInsertCount($uid,$num);
                $data['reward'] = $num;
                $ret = true;
                break;
            default: {
                return $this->returnError(7, "unkown item type");
                break;
            }
        }
        if (!$ret) {
            //如果使用失败则退还物品
            $this->add_user_item($uid, $item_id, 1, "物品使用失败回滚");
            return $this->returnError(8, "use this item to fail");
        }

        $data['uid'] = $uid;
        return $this->returnData($data);
    }

    //随机奖品规则 -1:规则错误 -2:权重总和错误 >0:itemid
    private function rand_reward($rule)
    {
        $sum     = 0;
        $rand    = mt_rand(1, 100000);
        $reward  = 0;
        $have_bd = false;
        //解析兑换比例
        $rand_rules = $this->get_rand_item_rule($rule);
        foreach ($rand_rules as $item) {
            $item_id = $item['item_id'];
            $weight  = $item['rand'];
            $sum     += $weight;
            $rand    -= $weight;
            if ($reward == 0 && $rand <= 0) {
                $reward  = $item_id;
                $have_bd = ($item['is_notice'] == 1);
            }
        }
        if ($sum == 100000) {
            return array($reward, $have_bd);
        } else {
            return array(-2, $have_bd);
        }
    }

}
